using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using AdventureWorks.Exceptions;

namespace AdventureWorks.Entities
{
	public class Customer : Entity
	{

		[Column("CustomerID")]
		public override Int32 ID { get; set; }

		public String Title { set; get; }

		[Display(Name = "First Name")]
		[Required]
		[MaxLength(50)]
		public String FirstName { get; set; }

		[Display(Name = "Middle Name")]
		[MaxLength(50)]
		public String MiddleName { get; set; }

		[Display(Name = "Last Name")]
		[Required]
		[MaxLength(50)]
		public String LastName { get; set; }

		[MaxLength(10)]
		public String Suffix { get; set; }

		[Display(Name = "Company Name")]
		[MaxLength(128)]
		public String CompanyName { get; set; }

		[Display(Name = "Sales Person")]
		[MaxLength(256)]
		public String SalesPerson { get; set; }

		[Display(Name = "Email Address")]
		[MaxLength(50)]
		public String EmailAddress { get; set; }

		[MaxLength(25)]
		public String Phone { get; set; }

		[Required]
		[MaxLength(128)]
		public String PasswordHash { get; set; }

		[Required]
		[MaxLength(10)]
		public String PasswordSalt { get; set; }

		[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
		public override DateTime ModifiedDate { get; set; }

		[NotMapped]
		[Display(Name="Full Name")]
		public String FullName { 
			get { return Title + " " + FirstName + " " + MiddleName + " " + LastName + " " + Suffix; }
			private set { }
		}

		public virtual ICollection<Address> Addresses { get; set; }

		public override Boolean Validate()
		{

			if (String.IsNullOrEmpty(FirstName))
				ThrowInvalidEntityException(ExceptionMessages.FirstNameInvalid);

			if (String.IsNullOrEmpty(LastName))
				ThrowInvalidEntityException(ExceptionMessages.LastNameInvalid);

			if (String.IsNullOrEmpty(PasswordHash))
				ThrowInvalidEntityException(ExceptionMessages.PwHashInvalid);

			if (String.IsNullOrEmpty(PasswordSalt))
				ThrowInvalidEntityException(ExceptionMessages.PwSaltInvalid);

			if (ModifiedDate == DateTime.MinValue)
				ModifiedDate = DateTime.Now;

			return true;
		}

		public override string ToString()
		{
			return FullName;
		}
	}
}

